package com.kuaimacode.kframework.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
import com.kuaimacode.kframework.util.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

public class AppolloDateDeserializer extends DateDeserializers.DateDeserializer {

    @Override
    protected Date _parseDate(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
        if (StringUtils.isEmpty(jsonParser.getText())) {
            return this.getNullValue(ctxt);
        } else {
            if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) {
                String str = jsonParser.getText().trim();
                if (this._customFormat != null) {
                    synchronized (this._customFormat) {
                        Date date;
                        try {
                            date = this._customFormat.parse(str);
                        } catch (ParseException var7) {
                            throw new IllegalArgumentException("Failed to parse Date value \'" + str + "\' (format: \"" + this._formatString + "\"): " + var7.getMessage());
                        }
                        return date;
                    }
                }
                return DateUtil.parse(str);
            } else {
                throw new IllegalArgumentException("Failed to parse Date value, type must be string");
            }
        }
    }
}
